package com.tanxi521.tnlevel.placeholder;

import com.tanxi521.tnlevel.TnLevel;
import com.tanxi521.tnlevel.dao.SQLManager;
import com.tanxi521.tnlevel.listener.TnLevelListener;
import com.tanxi521.tnlevel.pojo.PlayerLevel;
import com.tanxi521.tnlevel.utils.LevelCounter;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;

public class TnLevelPlaceHolder extends PlaceholderExpansion {


    @Override
    public String onPlaceholderRequest(Player p, String identifier) {
        if (p != null && identifier.startsWith("level_")) {
            return getPlaceHolder(p, identifier);
        }
        return "";
    }

    public static String getPlaceHolder(Player p, String identifier) {
        final String uuid = p.getUniqueId().toString();
        PlayerLevel playerLevel = LevelCounter.getCache(uuid);
        switch (identifier) {
            case "level_current_level":
                return Integer.toString(playerLevel.getPlayerLevel());
            case "level_current_exp":
                return Long.toString(playerLevel.getPlayerExp());
            case "level_to_next_exp":
                return Long.toString(playerLevel.getPlayerExpToNextLevel());
            case "level_exp_get":
                return null != TnLevelListener.expGet.get(p.getName())
                        ? TnLevelListener.expGet.get(p.getName()).toString() : 0 + "";
        }
        return "";
    }

    @Override
    public String getIdentifier() {
        return "tn";
    }

    @Override
    public String getAuthor() {
        return TnLevel.getInstance().getDescription().getAuthors().toString();
    }

    @Override
    public String getVersion() {
        return TnLevel.getInstance().getDescription().getVersion();
    }

    @Override
    public boolean persist() {
        return true;
    }

    @Override
    public boolean canRegister() {
        return true;
    }
}
